#include "mycoin.h"
#include <QDebug>

MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
{

}

MyCoin::MyCoin(QString filePath, int x, int y, bool f)
{
    // 初始化定时器
    timer = new QTimer();
    connect(timer,&QTimer::timeout,this,[=](){
       static int num = 1;
       QPixmap pixmap;
       QString str = QString(":/img/res/Coin000%1.png").arg(flag?9-num:num);
       pixmap.load(str);
       this->setIcon(pixmap);
       this->setStyleSheet("QPushButton{Border:0px}");
       this->setIconSize(pixmap.size());
       this->setFixedSize(pixmap.size());
       if(num++>=this->max){
           num = this->min;
           isAnimation = false;
           timer->stop();
       }
    });

    // 按钮和翻转连接
    connect(this,&QPushButton::clicked,this,&MyCoin::changeFlag);

    QPixmap pixmap;
    bool ret = pixmap.load(filePath);
    if(!ret){
        qDebug() << "金币图片打开失败";
    }
    setXY(x,y);
    setFlag(f);
    this->setStyleSheet("QPushButton{Border:0px}");
    this->setFixedSize(pixmap.size());
    this->setIcon(pixmap);
    this->setIconSize(pixmap.size());


}

void MyCoin::setXY(int x, int y)
{
    posX = x;
    posY = y;
}

void MyCoin::setFlag(bool f)
{
    flag = f;
}

void MyCoin::changeFlag()
{
    isAnimation = true;
    timer->start(30); // 每30帧触发一次
    this->flag = !this->flag;


}

void MyCoin::mousePressEvent(QMouseEvent *ev)
{
    if(isAnimation | isWin)
        return;
    return QPushButton::mousePressEvent(ev);
}

int MyCoin::getX()
{
    return posX;
}

int MyCoin::getY()
{
    return posY;
}

bool MyCoin::getFlag()
{
    return flag;
}

void MyCoin::setWin(bool win)
{
    isWin = win;
}
